//
//  FlutterNativeFactory.m
//  flutter_plugin_demo2
//
//  Created by DaviD on 2022/8/18.
//

#import "FlutterNativeFactory.h"

@implementation FlutterNativeFactory{
    NSObject<FlutterBinaryMessenger>* _messenger;
  }

- (instancetype)initWithMessenger:(NSObject<FlutterBinaryMessenger>*)messenger {
  self = [super init];
  if (self) {
    _messenger = messenger;
  }
  return self;
}

- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
                                   viewIdentifier:(int64_t)viewId
                                        arguments:(id _Nullable)args {

    return [[FlutterNativeView alloc] initWithFrame:frame
                                viewIdentifier:viewId
                                     arguments:args
                               binaryMessenger:_messenger];
}
@end

@implementation FlutterNativeView {
   UIView *_view;
    UILabel *_textLable;
}

- (instancetype)initWithFrame:(CGRect)frame
               viewIdentifier:(int64_t)viewId
                    arguments:(id _Nullable)args
              binaryMessenger:(NSObject<FlutterBinaryMessenger>*)messenger {
  if (self = [super init]) {
    _view = [[UIView alloc] init];
//      double kScreenW = [UIScreen mainScreen].bounds.size.width;
//      double kScreenH = [UIScreen mainScreen].bounds.size.height;
      _view.frame = CGRectMake(0, 200, 200, 200);
      _view.backgroundColor = [UIColor redColor];
      NSLog(@"args = %@",args);
      if ([args isKindOfClass:[NSDictionary class]]) {
          NSString *key = args[@"key"];
          NSLog(@"key = %@");
          _nameLable.text = [NSString stringWithFormat:@"%@%@%@",@"我是",key,@"的UILabel"];
      }
      
      [_view addSubview:self.nameLable];
      
  }
  return self;
}

-(UILabel *)nameLable {
    if (!_nameLable) {
        _nameLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 150, 150)];
        _nameLable.text = @"初始化数据";
        _nameLable.textColor = [UIColor blueColor];
        _nameLable.font = [UIFont systemFontOfSize:15];
        
    }
    return _nameLable;
}

- (UIView*)view {
  return _view;
}

@end
